<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/phpqrcode/phpqrcode.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Yunyin extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_user_material_model", "table");
        $this->user_material="user_material";
    }

    /**
     * 普通列表
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        $arr['type']=1;
        //分类筛选
        if(!empty($_GET['join_type'])){
            $arr['join_type']=$_GET['join_type'];
        }
        //话题筛选
        if(!empty($_GET['join_type_sub'])){
            $arr['join_type_sub']=$_GET['join_type_sub'];
        }
        //时间搜索
        if(!empty($_GET['time'])) {
            $get_time=$this->getStartAndEndTime("get");
            $start_time=strtotime($get_time[0]);
            $end_times=strtotime($get_time[1]);
            $end_time=$end_times+86400;
            $start=date("Y-m-d H:i:s",$start_time);
            $end=date("Y-m-d H:i:s",$end_time);
            $arr['start_time'] =$start;
            $arr['end_time'] =$end;
        }

        $res=$this->table->index($arr);
        //查询分类
        $type=$this->table->types(1);
        //查询话题
        $type_sub=$this->table->type_sub("",2);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
//                foreach($type as $kk=>$vv){
//                    if($v['quality_join_type']==$vv['id']){
//                        $v['quality_join_type_name']=$vv['name'];
//                    }
//                }
                foreach($type_sub as $kkk=>$vvv){
                    if($v['join_type_sub']==$vvv['id']){
                        $v['join_type_sub_name']=$vvv['sub_title'];
                    }
                }
            }
        }
        $data['data']=$res;
        $data['html_type']=1;
        $data['types']=$type;
        $data['type_sub']=$this->table->type_sub("",2);
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $this->layout("user_material/yunyin/index",$data);
    }

    /**
     * 精选列表
     */
    public function quality(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        $arr['type']=2;
        //分类筛选
        if(!empty($_GET['join_type'])){
            $arr['join_type']=$_GET['join_type'];
        }
        //话题筛选
        if(!empty($_GET['join_type_sub'])){
            $arr['join_type_sub']=$_GET['join_type_sub'];
        }
        //时间搜索
        if(!empty($_GET['time'])) {
            $get_time=$this->getStartAndEndTime("get");
            $start_time=strtotime($get_time[0]);
            $end_times=strtotime($get_time[1]);
            $end_time=$end_times+86400;
            $start=date("Y-m-d H:i:s",$start_time);
            $end=date("Y-m-d H:i:s",$end_time);
            $arr['start_time'] =$start;
            $arr['end_time'] =$end;
        }

        $res=$this->table->index($arr);
        //查询分类
        $type=$this->table->types(1);
        //查询话题
        $type_sub=$this->table->type_sub("",2);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
//                foreach($type as $kk=>$vv){
//                    if($v['quality_join_type']==$vv['id']){
//                        $v['quality_join_type_name']=$vv['name'];
//                    }
//                }
                foreach($type_sub as $kkk=>$vvv){
                    if($v['join_type_sub']==$vvv['id']){
                        $v['join_type_sub_name']=$vvv['sub_title'];
                    }
                }
            }
        }

        $data['data']=$res;
        $data['html_type']=2;
        $data['types']=$type;
        $data['type_sub']=$this->table->type_sub("",2);
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $this->layout("user_material/yunyin/quality",$data);
    }

    /**
     * 移入精选
     */
    public function join(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['quality']=2;
        $res=$this->table->update("user_material",$data,["id"=>$id]);
        if($res){
            response(0,"移入精选成功");
        }else {
            response(-1,"移入精选失败");
        }
    }

    /**
     * 移出精选
     */
    public function move(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['quality']=1;
        $data['quality_order']=999999999;
        $res=$this->table->update("user_material",$data,["id"=>$id]);
        if($res){
            response(0,"移出精选成功");
        }else {
            response(-1,"移出精选失败");
        }
    }

    /**
     * 弃用
     */
    public function drop(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $user_material=$this->table->one($this->user_material,['id'=>$id]);
        if($user_material['is_user_zj']==2){
            //如果是用户上传，先改变这个user_material表为弃用，再去改变精品视频表为弃用
            $data1['quality']=1;
            $data1['quality_order']=999999999;
            $data1['examine']=1;
            $data2['online']=1;
            $res=$this->table->update_quality_user($data1,$data2,$id);
        }else if($user_material['is_user_zj']==1){
            //如果是用户上传，直接把这条变为弃用就行
            $data['quality']=1;
            $data['quality_order']=999999999;
            $data['examine']=1;
            $res=$this->table->update("user_material",$data,["id"=>$id]);
        }
        if($res){
            response(0,"弃用成功");
        }else {
            response(-1,"弃用失败");
        }
    }

    /**
     * 评分修改
     */
    public function update_pingfen(){
        $id=$_POST['id'];
        $pingfen=$_POST['pingfen'];
        if(empty($id) || empty($pingfen)){
            response(-1,"缺少参数");
        }
        if(!is_numeric($pingfen)){
            response(-1,"请填写数字");
        }
        if($pingfen <0 || $pingfen >5){
            response(-1,"评分在1-5之间");
        }
        //先去查询精品表是否有这个视频
        $res=$this->table->update($this->user_material,['user_score'=>$pingfen],['id'=>$id]);
        if($res){
            response(0,"修改成功");
        }else {
            response(-1,"修改失败");
        }
    }


    /**
     * 已挑选视频排序
     */
    public function sort(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"参数不能为空");
        }
        $res=$this->table->one("user_material",["id"=>$id]);
        if($res['quality_order']==999999999 || empty($res['quality_order'])){
            $order=0;
        }else{
            $order=$res['quality_order'];
        }
        if($res){
            $html="";
            $html.="<br>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">当前排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"title\" value=\"{$order}\" placeholder=\"请输入排序\" class=\"layui-input\" disabled='disabled'>";
            $html.="</div>";
            $html.="</div>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">更改排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"\" placeholder=\"请输入排序\" class=\"layui-input edit_paixu\">";
            $html.="</div>";
            $html.="</div>";
            response(0,"成功",$html);
        }else{
            response(-1,"失败");
        }
    }

    /**
     * 保存排序
     */
    public function edit_sort(){
        if(empty($_POST['id'])){
            response(-1,"参数不能为空");
        }
        if(!is_numeric($_POST['paixu'])){
            response(-1,"排序值不是数字");
        }
        if($_POST['paixu']<0 || $_POST['paixu']>100){
            response(-1,"排序值不合法");
        }
        if($_POST['paixu']==0 || empty($_POST['paixu'])){
            $_POST['paixu']=999999999;
        }
        $old_sort=$this->table->one("user_material",['id'=>$_POST['id']],"quality_order");
        $res=$this->table->update_sort($_POST['paixu'],$_POST['id'],$old_sort['quality_order']);
        if($res){
            response(0,"排序成功");
        }else{
            response(-1,"排序失败");
        }
    }

    /**
     * 获取分类
     */
    public function types(){
        $biaoqian=$this->table->types();
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"获取失败",$biaoqian);
        }
    }

    /**
     * 获取话题
     */
    public function type_sub(){
        $where['sub_type']=$_POST['type_id'];
        $biaoqian=$this->table->type_sub($where);
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"当前分类下没有话题",$biaoqian);
        }
    }
}